#ifndef __DH_PROTOCOL_H__
#define __DH_PROTOCOL_H__

#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>

// 数据操作命令 客户端2服务端
// 不过期数据命令
#define DH_PROTOCOL_COMM_GET "G"
#define DH_PROTOCOL_COMM_TRYSET "TS"
#define DH_PROTOCOL_COMM_SET "S"
#define DH_PROTOCOL_COMM_DEL "D"

// 过期数据命令头
// e.g.:
//      GL18h
//      GL18h30m
#define DH_PROTOCOL_COMM_GETLRU "GL"
#define DH_PROTOCOL_COMM_TRYSETLRU "TSL"
#define DH_PROTOCOL_COMM_SETLRU "SL"
#define DH_PROTOCOL_COMM_DELLRU "DL"

// 服务器返回命令 服务端2客户端
#define DH_PROTOCOL_COMM_ERR "ERR"
#define DH_PROTOCOL_COMM_OK "OK"
#define DH_PROTOCOL_COMM_RES "RES"
#define DH_PROTOCOL_COMM_PONG "PONG"

#define DH_PROTOCOL_COMM_LEN 8
#define DH_PROTOCOL_LRU_LEN 8
#define DH_PROTOCOL_DATA_LEN 256
#define DH_PROTOCOL_DATA_MAX_LEN (DH_PROTOCOL_DATA_LEN * UINT8_MAX)
#define DH_PROTOCOL_HEAD 0x9E

    typedef struct dh_protocol_st
    {
        uint8_t head0;
        char comm[DH_PROTOCOL_COMM_LEN];
        char lru[DH_PROTOCOL_LRU_LEN];
        uint8_t unused;
        uint8_t pack_count;              // 总包数(最大值:255)
        uint8_t pack_idx;                // 包下标(最大值:总包数-1)
        char data[DH_PROTOCOL_DATA_LEN]; // key + value + [LRU] (255 + '\0')
        uint8_t head1;
    } dh_protocol_t;

    typedef void (*protocol_packs_make_cb_impl)(dh_protocol_t *buf, void *userdata);
    extern void protocol_pack_make(dh_protocol_t *buf, const char *comm, const char *data);
    extern int protocol_packs_make(dh_protocol_t *buf, const char *comm, const char *data, protocol_packs_make_cb_impl cb, void *userdata);



#ifdef __cplusplus
}
#endif

#endif